const {
  coupon
} = require("../../../utils/api")
import Toast from "../../../vant/weapp/toast/toast";
import clickreply from "../../../utils/clickreply";
// pages/index/order/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: true,
    wxpay: true,
    couponlist: true,
    user: [],
    orderDetail: [],
    add_time: '',
    orderType: 99,
    store_id: 0,
    fz: 0,
    coupon: {
      id: ''
    },

    order_on: '',
    from: true,
    counting: false,
    disabled: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setStorageSync('coupon', '')

    console.log(options)
    const that = this
    app.globalData.isUnload = false
    if (options.from === "renew") {
      console.log(options.from)
      that.getOrderDetail({
        store_id: options.store_id,
        order_on: options.order_on
      })
      that.setData({
        couponlist:false,
        from: false,
      })
    } else {
      that.setData({
        order_on: options.order_on,
        store_id: options.store_id,
      })
      that.getOrderDetail({
        store_id: options.store_id,
        order_on: options.order_on
      })
    }
    that.countDown(that, 120);
  },
  countDown(that, count) {
    //  console.log(count)
    if (count == 0) {
      that.setData({
        disabled: 'disabled',
        timeCountDownTop: '请重新选择时间下单',
        counting: false
      })
      return;
    }

    that.setData({
      counting: true,
      timeCountDownTop: '支付倒计时' + count + '秒',
    })

    setTimeout(function () {
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  onShow() {
    this.setData({
      coupon: wx.getStorageSync('coupon'),
    })

    this.setData({
      orderType: 99,
      balance: true,
      wxpay: true,
  
    })
    console.log(wx.getStorageSync('coupon'))

    if (wx.getStorageSync('coupon')) {
      if (this.data.coupon.type == 1) {
        //免费卷
        this.setData({
          orderType: 99,
          balance: false,
          wxpay: false,

        })

      } else {
        //折扣卷
        if (this.data.coupon.price - this.data.orderDetail.order.price < 0) {
          console.log(this.data.coupon.price - this.data.orderDetail.order.price)
          this.setData({
            orderType: 99,
            balance: false,
            wxpay: true,

          })
        } else {
          this.setData({
            orderType: 99,
            balance: true,
            wxpay: false,

          })
        }
      }



    }





  },
  onUnload() {
    const that = this
    console.log(`onUnload - MakeDelete1`, app.globalData.isUnload)
    if (app.globalData.isUnload) {
      // 页面手动跳转出去
      return
    } else {
      // 其他跳出方式 (back键)
      app.http.post(app.api.Delete, {
        order_on: that.data.order_on
      }, (res) => {
        wx.removeStorageSync('coupon')
      })
      // 是否需要删除订单
    }
  },
  // 包厢预定详情
  getOrderDetail(options) {
    console.log(options)
    const that = this
    app.http.post(app.api.OrderDetail, {
      order_on: options.order_on
    }, (res) => {
      console.log(res.data.order.add_time)
      let add_time = res.data.order.add_time.slice(0, 16)
      that.setData({
        user: res.data.user,
        orderDetail: res.data.order,
        fz: res.data.fz,
        add_time,
        store_id: options.store_id,
        order_on: options.order_on
      })
    })
  },

  getWaChat(e) {
    console.log(e)
    const that = this
    console.log(that.data.fz)
    wx.login({
      success(codes) {
        wx.hideLoading()
        if (codes.code) {
          app.http.post(app.api.wechat, {
            order_on: e.order_on,
            code: codes.code,
            type: e.type
          }, (re) => {
            console.log(re.data.parameter)
            wx.requestPayment({
              appid: re.data.parameter.appId,
              timeStamp: re.data.parameter.timeStamp,
              nonceStr: re.data.parameter.nonceStr,
              package: re.data.parameter.package,
              signType: re.data.parameter.signType,
              paySign: re.data.parameter.paySign,
              success(da) {
                if (da.errMsg == 'requestPayment:ok') {
                  wx.showModal({
                    title: '提示',
                    content: "预定成功",
                    showCancel: false,
                    complete: (res) => {
                      if (res.confirm) {
                        wx.reLaunch({
                          url: '/pages/order/index?status=1',
                        })
                      }
                    }
                  })

                }
              },
              fail(da) {
                console.log(da)
                if (!that.data.from) {
                  return
                }
                app.globalData.isUnload = false
                Toast({
                  type: 'fail',
                  message: '预定失败',
                  onClose: () => {

                  },
                });
              }
            })
          })

        }
      }
    })
  },
  // 包厢预定支付
  pay1() {
    // 0.余额支付 1.微信支付   2.优惠卷直接抵扣   3.优惠卷+微信支付
    if (clickreply.preventDuplicateClicks(1)) return;
    const that = this
    console.log(80, that.data.coupon.coupon_id)
    let coupon_id = ''
    // 优惠券
    if (that.data.coupon.coupon_id) {
      coupon_id = that.data.coupon.coupon_member_id
      if (that.data.coupon.type == 1) {
        that.setData({
          orderType: 2
        })
      } else {
        that.setData({
          orderType: 3
        })
      }
    }

    app.http.post(app.api.addOrder, {
      order_on: that.data.order_on,
      type: that.data.orderType,
      coupon_id
    }, (res) => {
      app.globalData.isUnload = true
      if (that.data.orderType == 2 || that.data.orderType == 0) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: res.msg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/order/index',
              })
            }
          }
        })
      } else {
        console.log('微信支付')
        console.log(res)
        that.getWaChat({
          order_on: res.data.order.order_on,
          type: 3
        })
        wx.hideLoading()
     
      }
      wx.removeStorageSync('coupon')
    })
  },
  // 续单预定支付
  pay2() {
    if (clickreply.preventDuplicateClicks(2)) return;
    // wx.showLoading({
    //     mask:true
    //   })
    const that = this
    let coupon_id = ''
    if (that.data.orderType == 0) {
      Toast.fail("请选择支付方式")
      return
    }
    // 优惠券
    if (that.data.coupon.id) {
      coupon_id = that.data.coupon.id
    }
    app.http.post(app.api.renewalAddorder, {
      order_on: that.data.order_on,
      type: that.data.orderType,
      coupon_id
    }, (res) => {
      app.globalData.isUnload = true
      if (that.data.orderType == 2) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: res.msg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/order/index',
              })
            }
          }
        })
      } else {
        that.getWaChat({

          order_on: res.data.order_on,
          type: 7
        })
      }

      wx.removeStorageSync('coupon')
    })
  },
  toCoupon() {
    const that = this
    console.log(that.data.store_id)
    wx.navigateTo({
      url: '/pages/person/maincoupon/index?from=storeOrder&order_on=' + this.data.order_on,
    })
  },
  choosType(e) {
    if (e.currentTarget.dataset.type == 2) {
      if (this.data.coupon.price < this.data.orderDetail.order.price) {
        this.setData({
          couponlist: false
        })
      }

    }
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        couponlist: true
      })
    }
    this.setData({
      orderType: e.currentTarget.dataset.type
    })
  }
})